"""文件操作复习"""

f1 = open('test.txt', 'r', encoding='utf-8')
lines_inf1 = f1.readlines()
f1.seek(0)
content = f1.read()
print(content)
print(lines_inf1)
f1.close()

data = [1, 2, 3, 4]
with open("output.csv", "w", encoding='utf-8') as f:
    # map()函数对data中的每个element都进行str()转换操作
    f.write(",".join(map(str, data)))

print("======")
# 遍历所有行的方法1
with open('test.txt', 'r', encoding='utf-8') as f:
    for line in f:
        print(line, end='')

print("======")
# 遍历所有行的方法2
with open('test.txt', 'r', encoding='utf-8') as f:
    for item in f.readlines():
        print(item, end='')

# 实现从一个文件中读取然后写入另一个文件
file_path = r'test1.txt'
with open('test.txt', 'r', encoding='utf-8') as src, open(file_path, 'w', encoding='utf-8') as target:
    for item in src:
        target.write(item)

# write writelines
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
with open('new_file.txt', 'w', encoding='utf-8') as new_f:
    # 用于写入一个列表到文件中
    new_f.writelines(lines)
